#!/bin/bash

# Fun-Sport Java 后端启动脚本

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${YELLOW}===============================================${NC}"
echo -e "${YELLOW}  Fun-Sport Java 后端启动脚本${NC}"
echo -e "${YELLOW}===============================================${NC}"

# 检查Java环境
echo -e "${BLUE}检查Java环境...${NC}"
if ! command -v java &> /dev/null; then
    echo -e "${RED}错误: 未找到Java环境，请先安装JDK 1.8+${NC}"
    exit 1
fi

JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
echo -e "${GREEN}✅ Java版本: $JAVA_VERSION${NC}"

# 检查Maven
echo -e "${BLUE}检查Maven环境...${NC}"
if ! command -v mvn &> /dev/null; then
    echo -e "${RED}错误: 未找到Maven，请先安装Maven 3.6+${NC}"
    exit 1
fi

MVN_VERSION=$(mvn -version | head -n 1)
echo -e "${GREEN}✅ $MVN_VERSION${NC}"

# 检查MySQL
echo -e "${BLUE}检查MySQL连接...${NC}"
if ! command -v mysql &> /dev/null; then
    echo -e "${YELLOW}⚠️  未找到MySQL客户端，跳过检查${NC}"
else
    # 从配置文件读取数据库配置
    DB_HOST="localhost"
    DB_PORT="3306"
    DB_USER="root"
    DB_PASSWORD="rootroot"
    DB_NAME="fun_sport"
    
    if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASSWORD" -e "USE $DB_NAME;" 2>/dev/null; then
        echo -e "${GREEN}✅ MySQL连接成功${NC}"
    else
        echo -e "${RED}错误: MySQL连接失败，请检查数据库配置${NC}"
        echo -e "${YELLOW}提示: 请确保MySQL已启动，并且数据库 '$DB_NAME' 已创建${NC}"
        exit 1
    fi
fi

# 检查Redis
echo -e "${BLUE}检查Redis连接...${NC}"
if ! command -v redis-cli &> /dev/null; then
    echo -e "${YELLOW}⚠️  未找到Redis客户端，跳过检查${NC}"
else
    if redis-cli ping > /dev/null 2>&1; then
        echo -e "${GREEN}✅ Redis连接成功${NC}"
    else
        echo -e "${YELLOW}⚠️  Redis连接失败，请确保Redis已启动${NC}"
        echo -e "${YELLOW}提示: 可以使用 'redis-server' 或 'brew services start redis' 启动Redis${NC}"
    fi
fi

# 检查配置文件
echo -e "${BLUE}检查配置文件...${NC}"
if [ ! -f "src/main/resources/application.yml" ]; then
    if [ -f "src/main/resources/application.yml.example" ]; then
        echo -e "${YELLOW}配置文件不存在，从模板创建...${NC}"
        cp src/main/resources/application.yml.example src/main/resources/application.yml
        echo -e "${GREEN}✅ 配置文件已创建${NC}"
    else
        echo -e "${RED}错误: 配置文件不存在${NC}"
        exit 1
    fi
else
    echo -e "${GREEN}✅ 配置文件存在${NC}"
fi

# 编译项目
echo -e "${BLUE}编译项目...${NC}"
mvn clean package -DskipTests
if [ $? -ne 0 ]; then
    echo -e "${RED}错误: 项目编译失败${NC}"
    exit 1
fi
echo -e "${GREEN}✅ 项目编译成功${NC}"

# 启动应用
echo -e "${BLUE}启动应用...${NC}"
echo -e "${YELLOW}===============================================${NC}"

# 查找jar文件
JAR_FILE=$(find target -name "fun-sport-*.jar" | head -n 1)

if [ -z "$JAR_FILE" ]; then
    echo -e "${RED}错误: 未找到jar文件${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 找到jar文件: $JAR_FILE${NC}"
echo -e "${YELLOW}===============================================${NC}"
echo -e "${GREEN}🚀 应用启动中...${NC}"
echo -e "${YELLOW}===============================================${NC}"

# 启动应用
java -jar "$JAR_FILE" \
    --spring.profiles.active=dev \
    --server.port=8000

echo -e "${YELLOW}===============================================${NC}"
echo -e "${GREEN}✅ 应用已停止${NC}"
echo -e "${YELLOW}===============================================${NC}"

